package com.example.gustavo.hello.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.example.gustavo.hello.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.io.IOException;

public class CropImageActivity extends ActionBarActivity {
    private File file;
    private ImageView imageView;
    private static final int GET = 0;
    private static final int CROP = 1;
    private String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "temp.jpeg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop_image);

        imageView = (ImageView) findViewById(R.id.iv_center);


        createFile();

    }

    private void createFile() {
        file = new File(path);

        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public void crop(View view){

        Intent intent = new Intent(Intent.ACTION_PICK);
//                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        //        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image");
//                intent.setType("audio");//选取音频 + Intent.ACTION_GET_CONTENT
        startActivityForResult(intent, GET);

                /*Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                intent.setType("image*//*");
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 200);
                intent.putExtra("scale", true);
                intent.putExtra("return-data", false);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                intent.putExtra("noFaceDetection", true); // no face detection
                startActivityForResult(intent, 1);*/




    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {

            switch (requestCode) {
                case GET:

//            Uri uri = data.getData();
//            Bitmap bitmap = null;//获取图片
//            try {
//                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
////                BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);//bitmap转drawable
//                imageView.setImageBitmap(bitmap);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }


                    Intent intent = new Intent("com.android.camera.action.CROP");
                    intent.setDataAndType(data.getData(), "image/*");
                    intent.putExtra("outputX", 512);
                    intent.putExtra("outputY", 512);
                    intent.putExtra("aspectX", 1);//宽高比
                    intent.putExtra("aspectY", 1);
                    intent.putExtra("scale", true);
                    intent.putExtra("scaleUpIfNeeded", true);
                    intent.putExtra("crop", "true");
                    intent.putExtra("noFaceDetection", true);
                    intent.putExtra("return-data", false);
                    intent.putExtra("output", Uri.fromFile(file));
                    intent.putExtra("outputFormat", "JPEG");
                    startActivityForResult(intent, CROP);
                    break;

                case CROP:
                    if (file != null && file.exists()) {
//                        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                        ImageLoader.getInstance().displayImage("file:///"+file.getAbsolutePath(),imageView);
//                        imageView.setImageBitmap(bitmap);
                    }


                    break;
            }
        }


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_crop_image, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
